/* PexifTool - $Revision$
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id$
 */
package de.pd.pjim2.controller.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import de.pd.pjim2.bean.PJFolderChangesBean;
import de.pd.pjim2.controller.PJWindowController;
import de.pd.pjim2.controller.gui.dialog.ChangesPreviewDialog;
import de.pd.pjim2.util.FileUtil;

/**
 * @author petros
 * 
 */
public class FolderIconPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4040530140694889267L;

	private String iconFile;
	private PJFolderChangesBean changes;

	/**
	 * @param pIconFile
	 * @param pChanges
	 */
	public FolderIconPanel(String pIconFile, PJFolderChangesBean pChanges) {
		super();
		iconFile = pIconFile;
		changes = pChanges;

		ApplicationActionMap actionMap = Application.getInstance().getContext()
				.getActionMap(this);
		
		setLayout(null);
		setPreferredSize(new Dimension(200, 120));
		setBackground(new Color(245, 245, 245));

		// toolbar
		JToolBar toolbar = new JToolBar();
		toolbar.setBackground(getBackground());
		toolbar.setBounds(0, 0, 200, 40);
		toolbar.setFloatable(false);
		// show changes button
		JButton btnShowChanges = new JButton(actionMap.get("showChangesAction"));
		toolbar.add(btnShowChanges);
		
		// open folder button
		JButton btnOpenFolder = new JButton(actionMap.get("openFolderAction"));
		toolbar.add(btnOpenFolder);
		add(toolbar);
		
		
		JPanel details = new JPanel();
		//details.setBorder(new LineBorder(Color.green));
		details.setLayout(new GridLayout(1,	1));
		details.setBackground(getBackground());
		details.setBounds(0, toolbar.getY() + toolbar.getHeight() + 3, getPreferredSize().width, getPreferredSize().height - toolbar.getHeight());
		add(details);
		JLabel detailsLabel = new JLabel(pChanges.getHTMLFormattedDetails());
		details.add(detailsLabel);
		
	}

	public String getIconFile() {
		return iconFile;
	}

	public void setIconFile(String pIconFile) {
		iconFile = pIconFile;
	}

	public PJFolderChangesBean getChanges() {
		return changes;
	}

	public void setChanges(PJFolderChangesBean pChanges) {
		changes = pChanges;
	}

	// -------------------
	// Actions
	/** Shows the changes preview.
	 * 
	 */
	@Action
	public void showChangesAction() {
		JFrame mainFrame = PJWindowController.getApplication().getMainFrame();
		ChangesPreviewDialog cpd = new ChangesPreviewDialog(mainFrame, changes);
		cpd.setLocationRelativeTo(null);
		cpd.setVisible(true);
	}
	
	/** Opens the source folder.
	 * 
	 */
	@Action
	public void openFolderAction() {
		try {
			FileUtil.openFolder(new File(changes.getDir()));
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "Could not open directory", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
}
